package com.example.casper.Experiment2024.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.activity.BookDetailActivity;
import com.example.casper.Experiment2024.adapter.BookAdapter;
import com.example.casper.Experiment2024.entity.BookInfo;
import com.example.casper.Experiment2024.utils.BookModel;
import com.example.casper.Experiment2024.utils.SharedPrefs;
import com.example.casper.Experiment2024.widget.RecyclerViewWithContextMenu;

import java.util.ArrayList;
import java.util.List;

public class BookListFragment extends Fragment {

    private List<BookInfo> data = new ArrayList<>();
    private int selectPosition;
    private BookAdapter adapter;
    private RecyclerViewWithContextMenu rv;
    private ActivityResultLauncher<Intent> activityResultLauncher;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_list, container, false);

        SharedPrefs sharedPrefs = new SharedPrefs(requireContext());
        Boolean isFirst = (Boolean) sharedPrefs.get("isFirst", true);
        if (isFirst) {
            BookModel.init(requireContext());
            sharedPrefs.put("isFirst", false);
        }
        data = BookModel.getData(requireContext());

        rv = view.findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        adapter = new BookAdapter(data);
        rv.setAdapter(adapter);
        // 为RecyclerView注册ContextMenu
        registerForContextMenu(rv);

        adapter.setOnItemLongClickListener((view1, position) -> {
            selectPosition = position;
            // 打开ContextMenu
            requireActivity().openContextMenu(view1);
        });

        activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result.getResultCode() == 666) {
                this.data = BookModel.getData(requireContext());
                adapter = new BookAdapter(data);
                rv.setAdapter(adapter);
            }
        });
        return view;
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        RecyclerViewWithContextMenu.RecyclerViewContextInfo contextMenuInfo = (RecyclerViewWithContextMenu.RecyclerViewContextInfo) menuInfo;
        if (contextMenuInfo != null && contextMenuInfo.getPosition() >= 0) {
            BookAdapter adapter = (BookAdapter) rv.getAdapter();
            menu.setHeaderTitle(adapter.getItem(contextMenuInfo.getPosition()).getTitle());
            createMenu(menu);
        }
    }

    private void createMenu(ContextMenu menu) {
        int groupID = 0;
        int order = 0;
        int[] itemID = {1, 2, 3};
        for (int i = 0; i < itemID.length; i++) {
            switch (itemID[i]) {
                case 1:
                    menu.add(groupID, itemID[i], order, "添加");
                    break;
                case 2:
                    menu.add(groupID, itemID[i], order, "修改");
                    break;
                case 3:
                    menu.add(groupID, itemID[i], order, "删除");
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                Intent intentAdd = new Intent(requireContext(), BookDetailActivity.class);
                intentAdd.putExtra("operate", "add");
                intentAdd.putExtra("position", selectPosition);
                activityResultLauncher.launch(intentAdd);
                break;
            case 2:
                Intent intentModify = new Intent(requireContext(), BookDetailActivity.class);
                intentModify.putExtra("operate", "modify");
                intentModify.putExtra("bookName", data.get(selectPosition).getTitle());
                intentModify.putExtra("position", selectPosition);
                activityResultLauncher.launch(intentModify);
                break;
            case 3:
                data.remove(selectPosition);
                BookModel.saveData(requireContext(), data);
                adapter.notifyDataSetChanged();
                break;
            default:
                break;
        }
        return super.onContextItemSelected(item);
    }
}